﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Telerik.Windows.Controls;

namespace StatusPlan3
{
    /// <summary>
    /// Interaction logic for Window_User.xaml
    /// </summary>
    public partial class Window_User : Window
    {
        User user;
        Boolean isNew = false;
        public Window_User()
        {
            InitializeComponent();
            
        }

        public void showNew()
        {
            isNew = true;
            populateFrame();
            this.ShowDialog();
        }

        public void showEdit(User u)
        {
            user = u;
            populateFrame();
            this.ShowDialog();
        }

        public void populateFrame()
        {
            if (!isNew)
            {
                FirstNameTextBox.Text = user.firstName;
                if(user.lastName != null)
                    LastNameTextBox.Text = user.lastName;
                UserNameTextBox.Text = user.userName;
                if(user.email != null)
                    EmailTextBox.Text = user.email;
                if (user.changePassword)
                    ChangePasswordTextBox.IsChecked = false;

                if (user.windowsLogin != null)
                    WindowsAuthTextBox.Text = user.windowsLogin;

                Roles allRoles = StatusPlanData.statusPlanHandler.getAllRoles();
                foreach (Role r in allRoles)
                {
                    RadTreeViewItem rtvi = new RadTreeViewItem();
                    rtvi.Header = r.name;
                    rtvi.Tag = r;

                    foreach (Role r2 in user.roles)
                    {
                        if (r2.id == r.id)
                        {
                            rtvi.IsChecked = true;
                            break;
                        }
                    }
                    
                    RoleTreeView.Items.Add(rtvi);
                }
                RoleTreeView.DisplayMemberPath = "name";
            }
            else
            {
                ChangePasswordTextBox.IsChecked = true;
                ChangePasswordTextBox.IsEnabled = false;

                Roles allRoles = StatusPlanData.statusPlanHandler.getAllRoles();
                foreach (Role r in allRoles)
                {
                    RadTreeViewItem rtvi = new RadTreeViewItem();
                    rtvi.Header = r.name;
                    rtvi.Tag = r;
                    RoleTreeView.Items.Add(rtvi);
                }
            }
        }

        private Boolean validateFileds()
        {
            Boolean okToSave = true;
            String errorString = "";

            if (FirstNameTextBox.Text.Trim().Length == 0)
            {
                okToSave = false;
                errorString += "Tú verður noydd/ur at skriva eitt fornavn";
            }


            return okToSave;
        }

        private void OkButto_Click(object sender, RoutedEventArgs e)
        {
            if (validateFileds())
            {
                if (isNew)
                {
                    User u = new User();
                    u.firstName = FirstNameTextBox.Text;

                    if (LastNameTextBox.Text.Trim().Length > 0)
                        u.lastName = LastNameTextBox.Text;

                    u.userName = UserNameTextBox.Text;

                    if (EmailTextBox.Text.Trim().Length > 0)
                        u.email = EmailTextBox.Text;

                    u.password = "12345";
                    u.changePassword = ChangePasswordTextBox.IsChecked.Value;
                    
                    if(WindowsAuthTextBox.Text.Trim().Length > 0) 
                        u.windowsLogin = WindowsAuthTextBox.Text;

                    foreach (RadTreeViewItem rtvi in RoleTreeView.CheckedItems)
                    {
                        Role r = rtvi.Tag as Role;
                        u.roles.Add(r);
                    }

                    StatusPlanData.statusPlanHandler.saveUser(u);
                }
                else
                {

                    user.firstName = FirstNameTextBox.Text;

                    if (LastNameTextBox.Text.Trim().Length > 0)
                        user.lastName = LastNameTextBox.Text;
                    else
                        user.lastName = null;

                    user.userName = UserNameTextBox.Text;

                    if (EmailTextBox.Text.Trim().Length > 0)
                        user.email = EmailTextBox.Text;
                    else
                        user.email = null;

                    if (WindowsAuthTextBox.Text.Trim().Length > 0)
                        user.windowsLogin = WindowsAuthTextBox.Text;
                    else
                        user.windowsLogin = null;

                    user.changePassword = ChangePasswordTextBox.IsChecked.Value;

                    user.roles.Clear();
                    foreach (RadTreeViewItem rtvi in RoleTreeView.CheckedItems)
                    {
                        Role r = rtvi.Tag as Role;
                        user.roles.Add(r);
                    }

                    StatusPlanData.statusPlanHandler.saveUser(user);
                }

                this.Close();
            }
        }

        private void CancelButto_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
